﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using rock.Model;
using rock.UI.custom;
using rock.UI.common;
using System.IO;
using rock.BLL;

namespace rock.UI.houseinfo
{
    public partial class FormRentContractUpdate : Form
    {
        DalRENT_CONTRACT dalRENT_CONTRACT = new DalRENT_CONTRACT();
        DalHOUSE_ROOM dalHOUSE_ROOM = new DalHOUSE_ROOM();

        BllRENT_CONTRACT bllRENT_CONTRACT = new BllRENT_CONTRACT();

        ModelRENT_CONTRACT modelRENT_CONTRACT = new ModelRENT_CONTRACT();
        public ModelRENT_CONTRACT _ModelRENT_CONTRACT { get{return modelRENT_CONTRACT;} set { modelRENT_CONTRACT = value;} }

        public FormRentContractUpdate()
        {
            InitializeComponent();


            this.AcceptButton = this.button4;
            this.CancelButton = this.button5;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = global::rock.Properties.Resources.house;


           
            
        }

        private void FormRentContract_Load(object sender, EventArgs e)
        {
            //List<ComboBoxInfo> lists = new List<ComboBoxInfo>();
            //lists.Add(new ComboBoxInfo() { Id = Constant.RENT_CONTRACT_STATUS_SHENGXIAO+"", Name = "生效" });
            //lists.Add(new ComboBoxInfo() { Id =Constant.RENT_CONTRACT_STATUS_DONGZHI+"",    Name = "终止" });
            //lists.Add(new ComboBoxInfo() { Id = Constant.RENT_CONTRACT_STATUS_FEICHU + "",  Name = "废除" });
            List<ComboBoxInfo> lists = Constant.RENT_CONTRACT_STATUS_LIST;

            this.comboBox1STATUS.DisplayMember = "Name";
            this.comboBox1STATUS.ValueMember = "Id";
            this.comboBox1STATUS.DataSource = lists;

            //更新
            if (this._ModelRENT_CONTRACT.SEQID > 0)
            {
                
                this.textBox1CONTRACT_NUMBER.Text = this._ModelRENT_CONTRACT.CONTRACT_NUMBER;
                this.dateTimePicker1START_DATE.Text = this._ModelRENT_CONTRACT.START_DATE.ToShortDateString();
                this.dateTimePicker2STOP_DATE.Text = this._ModelRENT_CONTRACT.STOP_DATE.ToShortDateString();

               // this.selectedOk(this._ModelRENT_CONTRACT.HOUSE_ROOM_SEQID, 2);
               // this.selectedOk(this._ModelRENT_CONTRACT.RENT_CUSTOM_SEQID, 1);

              //  this.comboBox1STATUS.SelectedValue = this._ModelRENT_CONTRACT.STATUS + "";

                this.userControlImportCustomInfo2._SHOW_BUTTON = false;
                this.userControlImportCustomInfo2._CUSTOM_SEQID = this._ModelRENT_CONTRACT.RENT_CUSTOM_SEQID;
                this.userControlImportCustomInfo2.inits(this._ModelRENT_CONTRACT.RENT_CUSTOM_SEQID);
                this.userControlImportRoomInfo2._SHOW_BUTTON = false;
                this.userControlImportRoomInfo2._ROOM_SEQID = this._ModelRENT_CONTRACT.HOUSE_ROOM_SEQID;
                this.userControlImportRoomInfo2.inits(this._ModelRENT_CONTRACT.HOUSE_ROOM_SEQID);

                //正在生效的合同不充许更改状态
                if (this._ModelRENT_CONTRACT.STATUS == Constant.RENT_CONTRACT_STATUS_SHENGXIAO) {
                    this.comboBox1STATUS.Enabled = false;
                }

            }
            else 
            {
                //添加
                this.textBox1CONTRACT_NUMBER.Text = StringUtils.getContractNumber();

                if (this._ModelRENT_CONTRACT.HOUSE_ROOM_SEQID > 0) {
                   // this.selectedOk(this._ModelRENT_CONTRACT.HOUSE_ROOM_SEQID, 2);
                    this.userControlImportRoomInfo2._ROOM_SEQID = this._ModelRENT_CONTRACT.HOUSE_ROOM_SEQID;
                    this.userControlImportRoomInfo2.inits(this._ModelRENT_CONTRACT.HOUSE_ROOM_SEQID);
                }

                if (this._ModelRENT_CONTRACT.RENT_CUSTOM_SEQID > 0)
                {
                   // this.selectedOk(this._ModelRENT_CONTRACT.RENT_CUSTOM_SEQID, 1);
                    this.userControlImportCustomInfo2._CUSTOM_SEQID = this._ModelRENT_CONTRACT.RENT_CUSTOM_SEQID;
                    this.userControlImportCustomInfo2.inits(this._ModelRENT_CONTRACT.RENT_CUSTOM_SEQID);
                }
               
                this.comboBox1STATUS.SelectedValue = Constant.RENT_CONTRACT_STATUS_SHENGXIAO + "";
                this.userControlImportCustomInfo2._SHOW_BUTTON = true;
                this.userControlImportRoomInfo2._SHOW_BUTTON = true;


            }


            if (this._ModelRENT_CONTRACT.STATUS != 0)
            {
                this.comboBox1STATUS.SelectedValue = this._ModelRENT_CONTRACT.STATUS +"";
            }
            else
            {
                this.comboBox1STATUS.SelectedValue = Constant.RENT_CONTRACT_STATUS_SHENGXIAO +"";
            }


            //this.textBox2RENT_CUSTOM_SEQID_INFO
            
           
            //this.textBox3HOUSE_ROOM_SEQID_INFO
            this.textBox4MONTH_COUNT.Text = this._ModelRENT_CONTRACT.MONTH_COUNT+"";
            this.textBox5RENT_MONEY.Text = this._ModelRENT_CONTRACT.RENT_MONEY+"";
            this.textBox6FILE_PATH.Text  =this._ModelRENT_CONTRACT.FILE_PATH;
            
            
        }
        private Boolean check()
        {
            try
            {//dalRENT_CUSTOM
                StringUtils.checkIsDecimal(textBox5RENT_MONEY.Text, "计租单价");

                StringUtils.checkText(this.textBox1NOTE.Text, 0, 300, "备注信息");


                if (Decimal.Parse(textBox5RENT_MONEY.Text) <=0)
                {
                    throw new Exception("你还没有输入计租单价.");
                }

                if (this.userControlImportCustomInfo2._CUSTOM_SEQID <= 0) {
                    throw new Exception("请先选择客户信息");
                }

                if(this.userControlImportRoomInfo2._ROOM_SEQID<=0 ){
                    throw new Exception("请先选择房屋信息");
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "提示");
                return false;
            }
            return true;
        }


        private void button4_Click_1(object sender, EventArgs e)
        {
            if (!check())
            {
                return;
            }
            this._ModelRENT_CONTRACT.CONTRACT_NUMBER = this.textBox1CONTRACT_NUMBER.Text;
            this._ModelRENT_CONTRACT.HOUSE_ROOM_SEQID = this.userControlImportRoomInfo2._ROOM_SEQID;// Convert.ToInt32(this.textBox1HOUSE_ROOM_SEQID.Text);
            this._ModelRENT_CONTRACT.RENT_CUSTOM_SEQID =this.userControlImportCustomInfo2._CUSTOM_SEQID;// Convert.ToInt32(this.textBox3RENT_CUSTOM_SEQID.Text);
            this._ModelRENT_CONTRACT.MONTH_COUNT = Convert.ToInt32(this.textBox4MONTH_COUNT.Text);
            this._ModelRENT_CONTRACT.RENT_MONEY =Convert.ToDecimal( this.textBox5RENT_MONEY.Text);
            this._ModelRENT_CONTRACT.NOTE = this.textBox1NOTE.Text;
            this._ModelRENT_CONTRACT.STATUS = Convert.ToInt32(this.comboBox1STATUS.SelectedValue);
            this._ModelRENT_CONTRACT.START_DATE = Convert.ToDateTime(this.dateTimePicker1START_DATE.Text);
            this._ModelRENT_CONTRACT.STOP_DATE = Convert.ToDateTime(this.dateTimePicker2STOP_DATE.Text);
            this._ModelRENT_CONTRACT.FILE_PATH = this.textBox6FILE_PATH.Text; 

       
            
            //复制文件.
            
            //String filepath = this.textBox6FILE_PATH.Text;
            //if (filepath != null && filepath.Equals(""))
            //{
            //    FileUtils.CopyTo(filepath, "contact_dir/" + this._ModelRENT_CONTRACT.CONTRACT_NUMBER + ".doc");
            //}
            



            bllRENT_CONTRACT.updateOrAddUser(this._ModelRENT_CONTRACT);
            //if (dalRENT_CONTRACT.Exists(this._ModelRENT_CONTRACT.SEQID))
            //{
            //    dalRENT_CONTRACT.Update(this._ModelRENT_CONTRACT);
            //}
            //else
            //{
            //    dalRENT_CONTRACT.Add(this._ModelRENT_CONTRACT);
            //}
            //触发代理
            oneRefreshParentData(1);
            this.Close();
        }
   

        //代理
        public delegate void RefreshParentData(int pageNo);
        public RefreshParentData oneRefreshParentData;
        //取消
        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        ////选房间
        //private void button2_Click(object sender, EventArgs e)
        //{
        //    FormSeleteRoom fsc = new FormSeleteRoom();
        //    fsc.selectedOk = selectedOk;
        //    fsc.ShowDialog(this);
        //}
        ////选客户
        //private void button3_Click(object sender, EventArgs e)
        //{
        //    FormSelectCustom fsc = new FormSelectCustom();
        //    fsc.selectedOk = selectedOk;
        //    fsc.ShowDialog(this);
        //}
        //加文件
        private void button1_Click(object sender, EventArgs e)
        {
           DialogResult dr =  this.openFileDialog1.ShowDialog();
           if (dr == DialogResult.OK) {
               this.textBox6FILE_PATH.Text = openFileDialog1.FileName;
               //this.textBox6FILE_PATH.Text = openFileDialog1.SoftFileName;
               //openFileDialog1.FileName
           }
        }

    //    int SELECTED_CUSTOM_SEQID = 0;
    //    int SELECTED_ROOM_SEQID = 0;
       
        /// <summary>
        /// 1:CUSTOM 2:ROOM
        /// </summary>
        /// <param name="seqid"></param>
        /// <param name="type"></param>
        //public void selectedOk(int seqid , int type) {
        //    if (type == 1) {
        //        SELECTED_CUSTOM_SEQID = seqid;
        //        DalRENT_CUSTOM _DalRENT_CUSTOM = new DalRENT_CUSTOM();
        //        ModelRENT_CUSTOM _ModelRENT_CUSTOM = _DalRENT_CUSTOM.GetModel(seqid);
        //       // this.textBox2RENT_CUSTOM_SEQID_INFO.Text = "用户名:" + _ModelRENT_CUSTOM.NAME +";电话:"+ _ModelRENT_CUSTOM.PHONE;
        //       // this.textBox3RENT_CUSTOM_SEQID.Text = seqid+"";
            

        //    }
        //    else if (type == 2) {
        //        SELECTED_ROOM_SEQID = seqid;
        //        DalHOUSE_ROOM _DalHOUSE_ROOM = new DalHOUSE_ROOM();
        //        ModelHOUSE_ROOM _ModelHOUSE_ROOM = _DalHOUSE_ROOM.GetModel(seqid);
        //      //  this.textBox3HOUSE_ROOM_SEQID_INFO.Text = "房号:" + _ModelHOUSE_ROOM.NAME + ";楼层:" + _ModelHOUSE_ROOM.ROOM_FLOOR +";面积"+ _ModelHOUSE_ROOM.ROOM_SIZE;
        //      //  this.textBox1HOUSE_ROOM_SEQID.Text = seqid+"";
        //    }
            
        
        //}

        private void dateTimePicker2STOP_DATE_ValueChanged(object sender, EventArgs e)
        {
            checkDate();

        }

        private void dateTimePicker1START_DATE_ValueChanged(object sender, EventArgs e)
        {

            checkDate();
        }

        private void checkDate(){

            DateTime start = this.dateTimePicker1START_DATE.Value.Date;
            DateTime end = this.dateTimePicker2STOP_DATE.Value.Date;

           // DateTime now = new DateTime();

            if (end < start)
            {
                MessageBox.Show("结束必大于开始日期", "提示");
                this.dateTimePicker1START_DATE.Text = dateTimePicker2STOP_DATE.Text ;
                this.textBox4MONTH_COUNT.Text = 0 + "";
            }
            else {
                int ry = end.Year - start.Year;
                int rm = end.Month - start.Month;
                this.textBox4MONTH_COUNT.Text = ry*12 + rm + "";
            }
        }

        
    }
}
